import {
  SELECTED_GOODS_COUNT,
  SELECTED_GOODS,
  SELECTED_GOODS_PRICE,
  USER_SEX
} from "./mutation-types"

import {
  getLocalStore,
  setLocalStore,
  removeLocalStore
} from '../config/global'

const getters = {
  shopCart: state => state.shopCart,
  userInfo: state => state.userInfo,
  userAddress: state => state.userAddress,
  // 1. 选中商品的数量
  SELECTED_GOODS_COUNT (state) {
    // 取出state中的商品数据
    const shopCart = state.shopCart
    const count = Object.values(shopCart).reduce((total, cur) => (cur.checked && total++, total), 0)
    return count
  },
  // 2.选中的商品
  SELECTED_GOODS (state) {
    const selectedGoodsCount = Object.values(state.shopCart).reduce((goodsCount, c) => (c.checked && goodsCount.push(state.shopCart[c.id]), goodsCount), [])
    return selectedGoodsCount
  },
  // 3.选中商品的价格
  SELECTED_GOODS_PRICE (state) {
    // 3.1 取到shopCart里面的数据遍历找到选中的goods计算总价
    const totalPrice = Object.values(state.shopCart).reduce((total, { checked, price, num }) => (checked && (total += price * num * 100), total), 0)
    return totalPrice
},
// 性别
  [USER_SEX] (state) {
    const sex = state.userInfo.sex
    const user_sex = {
      '1': '美女',
      '2': '帅哥',
      'default': '未填写'
    }
    return user_sex[sex] || user_sex['default']
  }
}

export default getters
